home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / WaveIndexUtility.c < prev    next >
Text File  |  1995-01-04  |  7KB  |  186 lines

  1. /* WaveIndexUtility.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveIndexUtility.h"
  31. #include "64BitMath.h"
  32. #include "Memory.h"
  33.  
  34.  
  35. /* perform wavetable indirection on an 8 bit wave */
  36. /* returns a value between MAX16BIT and MIN16BIT */
  37. signed long                    WaveTable8Bit(float Phase, FastFixedType TableIndex,
  38.                                             long NumTables, long Frames, signed char** Matrix)
  39.     {
  40.         FastFixedType            Index;
  41.         LongLongRec                FrameIndex;
  42.  
  43.         Index = (NumTables - 1) * TableIndex;
  44.         if (Index < 0)
  45.             {
  46.                 Index = 0;
  47.             }
  48.         else if (Index > Int2FastFixed(NumTables - 1))
  49.             {
  50.                 Index = Int2FastFixed(NumTables - 1);
  51.             }
  52.         Double2LongLong(Phase,&FrameIndex);
  53.         if (FastFixed2Int(Index) == NumTables - 1)
  54.             {
  55.                 /* this is done in case the wave table index is at the maximum, */
  56.                 /* in which case there is no table+1 to interpolate with. */
  57.                 signed char*                WaveData;
  58.  
  59.                 FastFixedType                LeftWeight;
  60.                 long                                ArraySubscript;
  61.                 signed long                    LeftValue;
  62.                 signed long                    RightValue;
  63.  
  64.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index)]),
  65.                     sizeof(Matrix[FastFixed2Int(Index)]));
  66.                 WaveData = Matrix[FastFixed2Int(Index)];
  67.  
  68.                 LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  69.                 ArraySubscript = LongLongHighHalf(FrameIndex) & (Frames - 1);
  70.                 /* L+F(R-L) */
  71.                 LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  72.                 RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  73.                 return LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  74.             }
  75.          else
  76.             {
  77.                 signed char*                WaveData0;
  78.                 signed char*                WaveData1;
  79.                 FastFixedType                Wave0Weight;
  80.  
  81.                 FastFixedType                LeftWeight;
  82.                 long                                ArraySubscript;
  83.                 signed long                    Left0Value;
  84.                 signed long                    Right0Value;
  85.                 signed long                    Left1Value;
  86.                 signed long                    Right1Value;
  87.                 FastFixedType                Wave0Temp;
  88.  
  89.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index)]),
  90.                     sizeof(Matrix[FastFixed2Int(Index)]));
  91.                 WaveData0 = (signed char*)(Matrix[
  92.                     FastFixed2Int(Index)]);
  93.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index) + 1]),
  94.                     sizeof(Matrix[FastFixed2Int(Index) + 1]));
  95.                 WaveData1 = Matrix[FastFixed2Int(Index) + 1];
  96.                 Wave0Weight = Index & FASTFIXEDFRACTMASK;
  97.  
  98.                 LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  99.                 ArraySubscript = LongLongHighHalf(FrameIndex) & (Frames - 1);
  100.                 /* L+F(R-L) -- applied twice */
  101.                 Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  102.                 Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  103.                 Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  104.                 Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  105.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  106.                 return Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  107.                     * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  108.             }
  109.     }
  110.  
  111.  
  112. /* perform wavetable indirection on a 16 bit wave */
  113. /* returns a value between MAX16BIT and MIN16BIT */
  114. signed long                    WaveTable16Bit(float Phase, FastFixedType TableIndex,
  115.                                             long NumTables, long Frames, signed short** Matrix)
  116.     {
  117.         FastFixedType            Index;
  118.         LongLongRec                FrameIndex;
  119.  
  120.         Index = (NumTables - 1) * TableIndex;
  121.         if (Index < 0)
  122.             {
  123.                 Index = 0;
  124.             }
  125.         else if (Index > Int2FastFixed(NumTables - 1))
  126.             {
  127.                 Index = Int2FastFixed(NumTables - 1);
  128.             }
  129.         Double2LongLong(Phase,&FrameIndex);
  130.         if (FastFixed2Int(Index) == NumTables - 1)
  131.             {
  132.                 /* this is done in case the wave table index is at the maximum, */
  133.                 /* in which case there is no table+1 to interpolate with. */
  134.                 signed short*                WaveData;
  135.  
  136.                 FastFixedType                LeftWeight;
  137.                 long                                ArraySubscript;
  138.                 signed long                    LeftValue;
  139.                 signed long                    RightValue;
  140.  
  141.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index)]),
  142.                     sizeof(Matrix[FastFixed2Int(Index)]));
  143.                 WaveData = Matrix[FastFixed2Int(Index)];
  144.  
  145.                 LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  146.                 ArraySubscript = LongLongHighHalf(FrameIndex) & (Frames - 1);
  147.                 /* L+F(R-L) */
  148.                 LeftValue = WaveData[ArraySubscript];
  149.                 RightValue = WaveData[ArraySubscript + 1];
  150.                 return LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  151.             }
  152.          else
  153.             {
  154.                 signed short*                WaveData0;
  155.                 signed short*                WaveData1;
  156.                 FastFixedType                Wave0Weight;
  157.  
  158.                 FastFixedType                LeftWeight;
  159.                 long                                ArraySubscript;
  160.                 signed long                    Left0Value;
  161.                 signed long                    Right0Value;
  162.                 signed long                    Left1Value;
  163.                 signed long                    Right1Value;
  164.                 FastFixedType                Wave0Temp;
  165.  
  166.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index)]),
  167.                     sizeof(Matrix[FastFixed2Int(Index)]));
  168.                 WaveData0 = Matrix[FastFixed2Int(Index)];
  169.                 PRNGCHK(Matrix,&(Matrix[FastFixed2Int(Index) + 1]),
  170.                     sizeof(Matrix[FastFixed2Int(Index) + 1]));
  171.                 WaveData1 = Matrix[FastFixed2Int(Index) + 1];
  172.                 Wave0Weight = Index & FASTFIXEDFRACTMASK;
  173.  
  174.                 LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  175.                 ArraySubscript = LongLongHighHalf(FrameIndex) & (Frames - 1);
  176.                 /* L+F(R-L) -- applied twice */
  177.                 Left0Value = WaveData0[ArraySubscript];
  178.                 Right0Value = WaveData0[ArraySubscript + 1];
  179.                 Left1Value = WaveData1[ArraySubscript];
  180.                 Right1Value = WaveData1[ArraySubscript + 1];
  181.                 Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  182.                 return Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  183.                     * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  184.             }
  185.     }
  186.